package com.baosight.sczx.common.zm.cInterface.cs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.common.zm.domain.InitialDatas;

/**
 * 系统初始数据 接口
 * <p>Title: InitialData.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-1-18 上午11:43:19  严洪浩  Create
 *</pre>
 */
public class InitialData {
	
	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	private static InitialData instance = new InitialData();
	private static Map<String,List<InitialDatas>> resultData;

	private InitialData(){
		initData();//当第一次使用的时候初始化
	}
	
	/**
	 * 初始化数据把所有的系统初始数据都查出来
	 * 放到内存中
	 * ZMCS12 是主表 ZMCS13是从表
	 * <p>Function: initData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-18下午01:02:39 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private void initData(){
		if(resultData == null) {
			resultData = new HashMap<String,List<InitialDatas>>();
		}
		
		List<InitialDatas> _list = dao.query("tZMCSInitialData.initQuery", new HashMap<String, String>());
		for(int i = 0; i < _list.size(); i++){
			InitialDatas _data = (InitialDatas)_list.get(i);
			String fdNoZb = _data.getFdNoZb();//主表编号

			if(resultData.containsKey(fdNoZb)) {
				resultData.get(fdNoZb).add(_data);
			}
			else{
				List<InitialDatas> _result = new ArrayList<InitialDatas>();
				_result.add(_data);
				resultData.put(fdNoZb, _result);
			}
		}
		
		CmesConstants.INITIALDATA_STATUS = 0;
	}
	
	/**
	 * 根据编号获取EiBlock
	 * <p>Function: getEiBlockByNo</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-18下午01:59:31 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public EiBlock getEiBlockByNo(String fdNo){
		EiBlock block = new EiBlock(fdNo);

		EiColumn eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<InitialDatas> _result = resultData.get(fdNo);
		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map;
		
		if(_result != null)
		{
			for(int i = 0; i < _result.size(); i ++){
				InitialDatas _data = (InitialDatas)_result.get(i);
				_map = new HashMap<String, String>();
				_map.put("display", _data.getFdName());
				_map.put("value", _data.getFdKeyId());
				_list.add(_map);
			}
		}
		
		block.setRows(_list);
		return block;
	}
	
	/**
	 * 根据编号获取EiBlock，ifNull为True下拉列表添加"全部"，否则为“”
	 * <p>Function: getEiBlockByNo</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-18下午02:45:26 严洪浩
	 *    2011-2-14       14:05 张志芳   		增加对没有查到数据时的处理
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiBlock getEiBlockByNo(String fdNo, boolean isAll){
		EiBlock block = new EiBlock(fdNo);

		EiColumn eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<InitialDatas> _result = resultData.get(fdNo);
		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map;
		
		if(_result != null)
		{
			for(int i = 0; i < _result.size(); i ++){
				InitialDatas _data = (InitialDatas)_result.get(i);
				if(i == 0) {
					if(isAll) {
						_map = new HashMap<String, String>();
						_map.put("value", "");
						_map.put("display", "");
						_list.add(_map);
					}
				}
				_map = new HashMap<String, String>();
				_map.put("value", _data.getFdKeyId());
				_map.put("display", _data.getFdName());
				_list.add(_map);
			}
		}
		block.setRows(_list);
		return block;
	}
	
	/**
	 * 根据编号获取EiBlock，isAll为True下拉列表添加"全部"，否则为“”
	 * oldBlockNo为注册的blockNo，newBlockNo为新的BlockNo
	 * <p>Function: getEiBlockByNo</p>
	 * <p>Description: </p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-18下午02:45:26 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiBlock getEiBlockByNo(String oldBolckNo, String newBlockNo, boolean isAll){
		EiBlock block = new EiBlock(newBlockNo);

		EiColumn eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<InitialDatas> _result = resultData.get(oldBolckNo);
		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map;
		
		if(_result != null)
		{
			for(int i = 0; i < _result.size(); i ++){
				InitialDatas _data = (InitialDatas)_result.get(i);
				if(i == 0) {
					if(isAll) {
						_map = new HashMap<String, String>();
						_map.put("value", "");
						_map.put("display", "");
						_list.add(_map);
					}
				}
				_map = new HashMap<String, String>();
				_map.put("value", _data.getFdKeyId());
				_map.put("display", _data.getFdName());
				_list.add(_map);
			}
		}
		block.setRows(_list);
		return block;
	}
	
	/**
	 * 初始化该类
	 * <p>Function: getInstance</p>
	 * <p>Description: </p>
	 * <p>return value:InitialData</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-7上午07:55:35 严洪浩
	 * </p>
	 *
	 */
	public static InitialData getInstance(){
		//第一次访问时status可能为0也可能为1
		if(CmesConstants.INITIALDATA_STATUS == 1) {
			resultData = null;
			instance = new InitialData();
		}
		return instance;
	}
	
	/**
	 * 
	 * <p>Function: getMoreEiBlockByNo</p>
	 * <p>Description: 根据编号获取EiBlock，并且增加了一列备注，为了给该数据做一个较为详细的描述</p>
	 * <p>return value:EiBlock</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-1-11下午03:04:42 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes" })
	public EiBlock getMoreEiBlockByNo(String fdNo){
		EiBlock block = new EiBlock(fdNo);

		EiColumn eiColumn = new EiColumn("display");
		eiColumn.setType("C");
		block.addMeta(eiColumn);

		eiColumn = new EiColumn("value");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		eiColumn = new EiColumn("remark");
		eiColumn.setType("C");
		block.addMeta(eiColumn);
		
		List<InitialDatas> _result = resultData.get(fdNo);
		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map;
		
		if(_result != null)
		{
			for(int i = 0; i < _result.size(); i ++){
				InitialDatas _data = (InitialDatas)_result.get(i);
				_map = new HashMap<String, String>();
				_map.put("display", _data.getFdName());
				_map.put("value", _data.getFdKeyId());
				_map.put("remark", _data.getFdRemarks());
				_list.add(_map);
			}
		}
		
		block.setRows(_list);
		return block;
	}
}
